const express = require('express');
// const mysql = require("mysql");
const connection = require('../db') // 获取连接实例
const router = express.Router();

// // 创建与数据库的连接
// const db = mysql.createConnection({
//   host:'localhost',
//   user:"root",
//   password:'wan320911',
//   database:"newsql"    //数据库名称
// });
//
// db.connect((err)=>{
//   if(err) throw err;
//   console.log("连接成功");
// })
/* GET users listing. */
router.get('/getFoods', function(req, res, next) {
  // res.send('respond with a resource');
  let sql = "select * from foods";
  connection.query(sql,(err,result)=>{
    if(err){
      console.log(err);
    }else{
      res.send({
          status:'0',
          errCode: 0,
          msg:result
      });
    }
  })
});
router.post('/addFood', function(req, res, next) {
  // res.send('respond with a resource');
  const sql = "insert into foods(name,price,number, `from`, `to`) values(?,?,?,?,?)" //SQL语句
  // const sql ='insert into foods set name=?, price=?, number=?, from=?, to=?';
  const sqlParams = ["帝王蟹",'22',10, "北京", "上海"] // 动态参数
  connection.query(sql,sqlParams, (err,result)=>{
    if(err){
      console.log(err);
    }else{
      res.send('增加数据成功');
    }
  })
});
module.exports = router;
